/*!
    \file  main.c
    \brief running led demo

    \version 2019-6-5, V1.0.0, demo for GD32VF103
*/

/*
    Copyright (c) 2019, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include <gd32vf103r_start.h>
#include "gd32vf103.h"
#include "systick.h"
#include <rtthread.h>
#include "main.h"

#define QUEUE_RECV_LENGTH   32
#define QUEUE_MSG_NUMS      10

static rt_thread_t dynamic_thread = RT_NULL;
volatile static int key_value = 0;

int get_key_value(void)
{
	return key_value;
}

void set_key_value(int val)
{
	key_value = val;
}

void test_thread_entry(void *parameter)
{
	rt_kprintf("create test_thread success.\r\n");

	while(1)
	{
		rt_kprintf("test_thread running......\r\n");
		rt_thread_mdelay(1000);
	}
}

void key_process_thread_entry(void *parameter)
{
	rt_err_t uwRet = RT_EOK;
	uint8_t is_thread_exit = 0;
	rt_thread_t dynamic_test_thread = RT_NULL;
	rt_kprintf("create key_process_thread.\r\n");

	while(1)
	{
		if (0 == gd_eval_key_state_get(KEY_A))
		{
			rt_thread_mdelay(20);
			if (0 == gd_eval_key_state_get(KEY_A))
			{
				if (1 == is_thread_exit)
				{
					is_thread_exit = 0;
					// delete thread
					rt_thread_delete(dynamic_test_thread);
					rt_kprintf("delete test_thread.\r\n");
				}else
				{
					is_thread_exit = 1;
					// create thread
					dynamic_test_thread = rt_thread_create("test_thread", test_thread_entry, RT_NULL, 1024, 2, 10);
					rt_thread_startup(dynamic_test_thread);
				}
			}
		}
		rt_thread_mdelay(10);
	}
}

int main(void)
{
    rt_kprintf("start application.\r\n");
    gd_eval_led_init(LED1);
    gd_eval_led_init(LED2);
    gd_eval_led_on(LED2);
    /* configure the Tamper key which is used to reload FWDGT  */
    gd_eval_key_init(KEY_A, KEY_MODE_GPIO);
    delay_1ms(500);

	// create key_thread
	dynamic_thread = rt_thread_create("key_thread", key_process_thread_entry,
										RT_NULL, 1024, 2, 10);
	rt_thread_startup(dynamic_thread);

    while(1)
    {
    	gd_eval_led_on(LED1);
        /* insert 500 ms delay */
        rt_thread_mdelay(500);

        gd_eval_led_off(LED1);
        /* insert 500 ms delay */
        rt_thread_mdelay(500);
    }
}
